if (!window.VFG) {
  window.VFG = {};
}
var VFG = window.VFG;
VFG.Map = function(domId,options) {
	this.objects=new Map();
	this.ImageryLayerMap=new Map();//底图集合
	this.tilesetLayerMap=new Map();//底图集合
	this.modelLayerMap=new Map();//底图集合
	this.vmodelLayerMap=new Map();//底图集合
	this.entityLayerMap=new Map();//底图集合
	this.pointLayerMap=new Map();//底图集合
	this.collectionLayerMap=new Map();//底图集合
	this.polylineLayerMap=new Map();//底图集合
	this.measureLayerMap=new Map();//测量集合
	this.wallLayerMap=new Map();//测量集合
	
	let param={
			scene3DOnly:options.scene3DOnly||true,
			selectionIndicator: options.selectionIndicator||false,
			baseLayerPicker:options.baseLayerPicker||false,
			animation: options.animation||false,
			timeline: options.timeline||false,
			navigationHelpButton: options.navigationHelpButton||false,
			fullscreenButton: options.fullscreenButton||false,
			geocoder:options.geocoder||false,
			homeButton:options.homeButton||false,
			shouldAnimate: options.shouldAnimate||false,
			sceneModePicker: options.sceneModePicker||false,
			shouldAnimate:options.shouldAnimate||false,
			infoBox: options.infoBox||false,
			sceneMode: options.sceneMode || Cesium.SceneMode.SCENE2D,	
			clockViewModel:new Cesium.ClockViewModel(),
	};
	
	if(options.scene){
		param.scene={
			center:options.scene.center,
			showSun:options.scene.showSun||true,
			showMoon:options.scene.showMoon||true,
			showSkyAtmosphere:options.scene.showSkyAtmosphere||true,
			fog:options.scene.fog||true,
			fxaa:options.scene.fxaa||true,
			showSkyBox:options.scene.showSkyBox||true,
		}
		if(options.scene.skyBox && options.scene.skyBox.length==6){
			param.scene.skyBox=new Cesium.SkyBox({
	    	  sources: {
				  positiveX: skyBox[0],
				  negativeX: skyBox[1],
				  positiveY: skyBox[2],
				  negativeY: skyBox[3],
				  positiveZ: skyBox[4],
				  negativeZ: skyBox[5]
	    	  	}
    	  });
		}
		if(options.scene.globe){
			param.scene.globe={
				depthTestAgainstTerrain:options.scene.globe.depthTestAgainstTerrain||true,
				baseColor:options.scene.globe.baseColor||null,
			}
		}
		if(options.scene.cameraController){
			param.scene.cameraController={
				zoomFactor:options.scene.cameraController.zoomFactor||3,
				minimumZoomDistance:options.scene.cameraController.minimumZoomDistance||0,
				maximumZoomDistance:options.scene.cameraController.maximumZoomDistance||50000000,
				enableRotate:options.scene.cameraController.enableRotate||true,
				enableZoom:options.scene.cameraController.enableZoom||true,
			}
		}
	}
	
	if(options.contextOptions){
		param.contextOptions={}
		if(options.contextOptions.webgl){
			param.contextOptions.webgl={
				alpha:options.contextOptions.webgl.alpha||true,
				depth:options.contextOptions.webgl.depth||true,
				stencil:options.contextOptions.webgl.stencil||true,
				antialias:options.contextOptions.webgl.antialias||true,
				premultipliedAlpha:options.contextOptions.webgl.premultipliedAlpha||true,
				preserveDrawingBuffer:options.contextOptions.webgl.preserveDrawingBuffer||true,
				failIfMajorPerformanceCaveat:options.contextOptions.webgl.failIfMajorPerformanceCaveat||true,
			}
		}
	}
    
	param.imageryProvider=new Cesium.TileMapServiceImageryProvider({
		url:"assets/plugins/Cesium/Assets/Textures/NaturalEarthII",
		maximumLevel: 1,
	})
	
	Cesium.Ion.defaultAccessToken ="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJiZjliY2NhNS1iMThiLTQzZTYtOWM1ZS03NDMxMmY3YjIyNmUiLCJpZCI6MjAwNDcsInNjb3BlcyI6WyJhc2wiLCJhc3IiLCJhc3ciLCJnYyJdLCJpYXQiOjE1ODMxOTk3NTl9.lzdqsXU24bvovmmaRPXcD3jmSH0WmYTwmX6isCx8qfA";
	this.viewer = new Cesium.Viewer(domId,param);
	this.viewer.cesiumWidget.creditContainer.style.display = "none";
	this.viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_DOUBLE_CLICK);
	this.viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK)
	
	//Cesium 禁止/允许相机进入地下
	this.viewer.scene.screenSpaceCameraController.enableCollisionDetection = false; //true 禁止 false 允许
	
	//解决模糊问题
	if(Cesium.FeatureDetection.supportsImageRenderingPixelated()){//判断是否支持图像渲染像素化处理
		this.viewer.resolutionScale = window.devicePixelRatio;
	}
	//是否开启抗锯齿
	this.viewer.scene.fxaa = true;
	this.viewer.scene.postProcessStages.fxaa.enabled = true;
	
	
	
	
//	this.viewer._cesiumWidget._supportsImageRenderingPixelated = Cesium.FeatureDetection.supportsImageRenderingPixelated();
//	this.viewer._cesiumWidget._forceResize = true;
//	if (Cesium.FeatureDetection.supportsImageRenderingPixelated()) {
//	    var vtxf_dpr = window.devicePixelRatio;
//	    // 适度降低分辨率
//	    while (vtxf_dpr >= 2.0) {
//	        vtxf_dpr /= 2.0;
//	    }
//	    this.viewer.resolutionScale = vtxf_dpr;
//	}
	
	

//	this.viewer.scene.globe.enableLighting = true; //关闭光照
//	this.viewer.scene.sun.true = 5; //太阳变大
//	this.viewer.shadows = true;//关闭阴影
//    this.viewer.scene.light = new Cesium.DirectionalLight({ //去除时间原因影响模型颜色
//    	  direction: new Cesium.Cartesian3(0.354925, -0.890918, -0.283358)
//    });
	
    this.viewer.scene.skyBox.show = false; 

    
    // 亮度设置
//    var stages = this.viewer.scene.postProcessStages;
//    this.viewer.scene.brightness =  this.viewer.scene.brightness || stages.add(Cesium.PostProcessStageLibrary.createBrightnessStage());
//    this.viewer.scene.brightness.enabled = true;
//    this.viewer.scene.brightness.uniforms.brightness = Number(1.0);
    
    
	this.viewer.scene.primitives.add(new Cesium.PointPrimitiveCollection(),0);
	this.viewer.scene.primitives.add(new Cesium.LabelCollection(),1);
	this.viewer.scene.primitives.add(new Cesium.BillboardCollection(),2);
	
//	 var collection = this.viewer.scene.postProcessStages;
//	 var silhouette = collection.add(Cesium.PostProcessStageLibrary.createBrightnessStage());
//	 silhouette.enabled = true;
//	silhouette.uniforms.brightness=1;// （调节亮度0-3最佳）
	let _this=this;
	this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
    this.handler.setInputAction(function (movement) {
    	if(Cesium.defined(_this.mouseMoveFun)){
    		_this.mouseMoveFun(movement);
    	}
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
	
	return this;
}

/**
 * 获取主键ID
 */
VFG.Map.prototype.getUuid = function() {
	return VFG.Util.getUuid();
};

/**
 * 将三维球定位到中国
 */
VFG.Map.prototype.flyToChina = function(complete) {
	this.viewer.camera.flyTo({
	    destination: Cesium.Cartesian3.fromDegrees(103.84, 31.15, 17850000),
	    orientation: {
	        heading :  Cesium.Math.toRadians(348.4202942851978),
	        pitch : Cesium.Math.toRadians(-89.74026687972041),
	        roll : Cesium.Math.toRadians(0)
	    },
	    complete:function callback() {
	    	if(complete){
	    		complete();
	    	}
	    }
	});
}

VFG.Map.prototype.openFlyAnimation=function(param) {
    var that = this;
    var viewer = this.viewer;
    var view = this.getCameraView(); //默认为原始视角
    
    var position=param.position;
    var orientation=param.orientation||{};

    if(!position){
    	return;
    }
    
    viewer.camera.setView({
        destination: Cesium.Cartesian3.fromDegrees(-85.16, 13.71, 23000000.0),
    });
    
    viewer.camera.flyTo({
        destination: Cesium.Cartesian3.fromDegrees(position.x, position.y, 23000000.0),
        duration: 2,
        easingFunction: Cesium.EasingFunction.LINEAR_NONE,
        complete: function complete() {
            var z = Cesium.defaultValue(position.z, 90000);
            if (z < 200000 && orientation.pitch != -90) {
                z = z * 1.5 + 8000;
                viewer.camera.flyTo({
                    destination: Cesium.Cartesian3.fromDegrees(position.x, position.y, z),
                    complete: function complete() {
                    	viewer.camera.flyTo({
                            destination : Cesium.Cartesian3.fromDegrees(position.x*1, position.y*1,position.z*1),
                            orientation : {
                                heading : Cesium.Math.toRadians(orientation.pitch?orientation.heading*1:0),
                                pitch : Cesium.Math.toRadians(orientation.pitch?orientation.pitch*1:0),
                                roll :Cesium.Math.toRadians(orientation.pitch?orientation.roll*1:0)
                            },
	                    	complete: function complete() {
	                            that._isFlyAnimation = false;
	                            if (param.complete) param.complete();
	                        }
                        });
                    }
                });
            } else {
            	viewer.camera.flyTo({
                    destination : Cesium.Cartesian3.fromDegrees(position.x*1, position.y*1,position.z*1),
                    orientation : {
                        heading : Cesium.Math.toRadians(orientation.pitch?orientation.heading*1:0),
                        pitch : Cesium.Math.toRadians(orientation.pitch?orientation.pitch*1:0),
                        roll :Cesium.Math.toRadians(orientation.pitch?orientation.roll*1:0)
                    },
                	complete: function complete() {
                        that._isFlyAnimation = false;
                        if (param.complete) param.complete();
                    }
                });
            }
        }
    });
}

//提取相机视角范围参数 
VFG.Map.prototype.getCameraView=function() {
    var camera = this.viewer.camera;
    var position = camera.positionCartographic;
    var bookmark = {};
    bookmark.y = VFG.Util.formatNum(Cesium.Math.toDegrees(position.latitude), 6);
    bookmark.x = VFG.Util.formatNum(Cesium.Math.toDegrees(position.longitude), 6);
    bookmark.z = VFG.Util.formatNum(position.height, 2);
    bookmark.heading = VFG.Util.formatNum(Cesium.Math.toDegrees(camera.heading || -90), 1);
    bookmark.pitch = VFG.Util.formatNum(Cesium.Math.toDegrees(camera.pitch || 0), 1);
    bookmark.roll = VFG.Util.formatNum(Cesium.Math.toDegrees(camera.roll || 0), 1);
    return bookmark;
}

/**
 * 渲染对象
 */
VFG.Map.prototype.render = function(option,uri) {
	if(option && '0'==option.type){
		if(!this.ImageryLayerMap.has(option.id)){
			let entity=VFG.Render.getImageryProvider(this.viewer,option,uri);
			this.ImageryLayerMap.set(option.id,entity);
			return entity;
		}
	}
	else if(option && '1'==option.type){
		if(!this.tilesetLayerMap.has(option.id)){
			let entity=VFG.Render.getTModel(this.viewer,option,uri);
			this.tilesetLayerMap.set(option.id,entity);
			return entity;
		}
	}
	else if(option && '2'==option.type){
		if(!this.modelLayerMap.has(option.id)){
			let entity=VFG.Render.getPModel(this.viewer,option,uri);
			this.modelLayerMap.set(option.id,entity);
			return entity;
		}
	}
	else if(option && '3'==option.type){
		if(!this.vmodelLayerMap.has(option.id)){
			let entity=VFG.Render.getVModel(this.viewer,option,uri);
			this.vmodelLayerMap.set(option.id,entity);
			return entity;
		}
	}
	else if(option && 'ERoadMark'==option.type){
		if(!this.entityLayerMap.has(option.id)){
			let entity=VFG.Render.getERoadMark(this.viewer,option,uri);
			this.entityLayerMap.set(option.id,entity);
			return entity;
		}
	}
	else if(option && 'Point'==option.type){
		if(!this.pointLayerMap.has(option.id)){
			let entity=VFG.Render.getPointMark(this.viewer,option,uri);
			this.pointLayerMap.set(option.id,entity);
			return entity;
		}
	}
	else if(option && 'Polyline'==option.type){
		if(!this.polylineLayerMap.has(option.id)){
			let entity=VFG.Render.getEPolyline(this.viewer,option,uri);
			this.polylineLayerMap.set(option.id,entity);
			return entity;
		}
	}
	else if(option && 'Wall'==option.type){
		if(!this.wallLayerMap.has(option.id)){
			let entity=VFG.Render.getEWall(this.viewer,option,uri);
			this.wallLayerMap.set(option.id,entity);
			return entity;
		}
	}
}

VFG.Map.prototype.renders = function(list,uri) {
	for(var item of list){
		this.render(item,uri);
	}
}

/**
 * 添加点点集合
 */
VFG.Map.prototype.addPointToLayer= function(obj) {
	if(!this.pointLayerMap.has(obj.id)){
		this.pointLayerMap.set(obj.id,obj)
	}	
}

/**
 * 获取对象
 */
VFG.Map.prototype.getById = function(id) {
	if(this.objects.has(id)){
		return this.objects.get(id);
	}
	else if(this.tilesetLayerMap.has(id)){
		return this.tilesetLayerMap.get(id);
	}
	else if(this.ImageryLayerMap.has(id)){
		return this.ImageryLayerMap.get(id);
	}
	else if(this.modelLayerMap.has(id)){
		return this.modelLayerMap.get(id);
	}
	else if(this.vmodelLayerMap.has(id)){
		return this.vmodelLayerMap.get(id);
	}
	else if(this.entityLayerMap.has(id)){
		return this.entityLayerMap.get(id);
	}
	else if(this.pointLayerMap.has(id)){
		return this.pointLayerMap.get(id);
	}
	else if(this.polylineLayerMap.has(id)){
		return this.polylineLayerMap.get(id);
	}
}

/**
 * 移除对象
 */
VFG.Map.prototype.remove = function(id) {
	if(this.objects.has(id)){
		try{
			this.objects.get(id).destroy();
		}catch (e) {
		}finally{
			this.objects.delete(id);
		}
	}
	else if(this.ImageryLayerMap.has(id)){
		try{
			this.ImageryLayerMap.get(id).destroy();
		}catch (e) {
		}finally{
			this.ImageryLayerMap.delete(id);
		}
	}
	else if(this.tilesetLayerMap.has(id)){
		try{
			this.tilesetLayerMap.get(id).destroy();
		}catch (e) {
		}finally{
			this.tilesetLayerMap.delete(id);
		}
	}
	else if(this.modelLayerMap.has(id)){
		try{
			this.modelLayerMap.get(id).destroy();
		}catch (e) {
		}finally{
			this.modelLayerMap.delete(id);
		}
	}
	else if(this.vmodelLayerMap.has(id)){
		try{
			this.vmodelLayerMap.get(id).destroy();
		}catch (e) {
		}finally{
			this.vmodelLayerMap.delete(id);
		}
	}
	else if(this.entityLayerMap.has(id)){
		try{
			this.entityLayerMap.get(id).destroy();
		}catch (e) {
		}finally{
			this.entityLayerMap.delete(id);
		}
	}
	else if(this.pointLayerMap.has(id)){
		try{
			this.pointLayerMap.get(id).destroy();
		}catch (e) {
		}finally{
			this.pointLayerMap.delete(id);
		}
	}
	else if(this.polylineLayerMap.has(id)){
		try{
			this.polylineLayerMap.get(id).destroy();
		}catch (e) {
		}finally{
			this.polylineLayerMap.delete(id);
		}
	}
}

VFG.Map.prototype.removes = function(list) {
	for(let item of list){
		this.remove(item.id);
	}
}

/**
 * 清空
 */
VFG.Map.prototype.clearAll = function() {
	_this=this;
	_this.objects.forEach(function (value,key){
		try{
			_this.objects.get(key).destroy();
		}catch (e) {
		}
	});
	_this.objects.clear();
	_this.ImageryLayerMap.forEach(function (value,key){
		try{
			_this.ImageryLayerMap.get(key).destroy();
		}catch (e) {
		}
	});
	_this.ImageryLayerMap.clear();
	
	_this.modelLayerMap.forEach(function (value,key){
		try{
			_this.modelLayerMap.get(key).destroy();
		}catch (e) {
		}
	});
	_this.modelLayerMap.clear();
	
	_this.tilesetLayerMap.forEach(function (value,key){
		try{
			_this.tilesetLayerMap.get(key).destroy();
		}catch (e) {
		}
	});
	_this.tilesetLayerMap.clear();
	
	_this.vmodelLayerMap.forEach(function (value,key){
		try{
			_this.vmodelLayerMap.get(key).destroy();
		}catch (e) {
		}
	});
	_this.vmodelLayerMap.clear();
	
	_this.entityLayerMap.forEach(function (value,key){
		try{
			_this.entityLayerMap.get(key).destroy();
		}catch (e) {
		}
	});
	_this.entityLayerMap.clear();
	
	_this.polylineLayerMap.forEach(function (value,key){
		try{
			_this.polylineLayerMap.get(key).destroy();
		}catch (e) {
		}
	});
	_this.polylineLayerMap.clear();
	
}


VFG.Map.prototype.exportToImg=function(width,height){
	return VFG.Util.exportToImg(this.viewer,width,height);
}


/**
 * 绘制模型
 */
VFG.Map.prototype.drawPModel = function(option) {
	return new VFG.DrawPModel( this.viewer,option);
}

VFG.Map.prototype.createEditPModel = function(entity) {
	return new VFG.EditPModel(this.viewer,entity);
}

VFG.Map.prototype.createEditRoadMark = function(entity) {
	return new VFG.EditRoadMark(this.viewer,entity);
}

/**
 * 绘制视频模型
 */
VFG.Map.prototype.drawVModel = function(option) {
	return new VFG.DrawVModel( this.viewer,option);
}

VFG.Map.prototype.createEditVModel = function(entity) {
	return new VFG.EditVModel(this.viewer,entity);
}

/**
 * 绘制路面标注
 */
VFG.Map.prototype.drawRoadMark = function(option) {
	return new VFG.DrawRoadMark(this.viewer,option);
}



/**
 * 绘制点
 */
VFG.Map.prototype.drawPPoint = function(option) {
	return new VFG.DrawPPoint(this.viewer,option);
}

/**
 *编辑点 
 */
VFG.Map.prototype.createEditPPoint = function(option) {
	return new VFG.EditPPoint(this.viewer,option);
}

VFG.Map.prototype.createEditTModel = function(entity) {
	return new VFG.EditTModel(this.viewer,entity);
}

/**
 * 绘制矩形
 */
VFG.Map.prototype.drawRectangle = function(option) {
	return new VFG.DrawRectangle(this.viewer,option);
}

VFG.Map.prototype.drawPolyline = function(option) {
	return new VFG.DrawPolyline(this.viewer,option);
}

VFG.Map.prototype.createEditEPolyline = function(entity) {
	return new VFG.EditEPolyline(this.viewer,entity);
}

VFG.Map.prototype.drawPolygon = function(option) {
	return new VFG.DrawPolygon(this.viewer,option);
}

VFG.Map.prototype.drawWall = function(option) {
	return new VFG.DrawWall(this.viewer,option);
}

/**
 * 空间距离测量
 */
VFG.Map.prototype.measureLength = function() {
	let entity = new VFG.MeasureLength( this.viewer);
	this.measureLayerMap.set(VFG.Util.getUuid(),entity);
	return entity;
}

/**
 * 水平面积测量
 */
VFG.Map.prototype.measureArea = function() {
	let entity = new VFG.MeasureArea( this.viewer);
	this.measureLayerMap.set(VFG.Util.getUuid(),entity);
	return entity;
}

/**
 * 三角测量
 */
VFG.Map.prototype.measureTriangle = function() {
	let entity = new VFG.MeasureTriangle(this.viewer);
	this.measureLayerMap.set(VFG.Util.getUuid(),entity);
	return entity;
}


/**
 * 当前视角
 */
VFG.Map.prototype.getVisualAngle=function() {
	return VFG.Util.getVisualAngle(this.viewer)
};

/**
 * 预案漫游
 */
VFG.Map.prototype.planRoam=function(options) {
	return new VFG.PlanRoam(this.viewer,options)
};

/**
 * 飞行
 */
VFG.Map.prototype.flyTo=function(option) {
	VFG.Util.flyTo(this.viewer,option);
};

/**
 * 屏幕坐标转世界坐标
 */
VFG.Map.prototype.getScreenToC3=function(position, noPickEntity) {
	return VFG.Util.getScreenToC3(this.viewer,position, noPickEntity);
}

/**
 * 世间坐标转经纬度
 */
VFG.Map.prototype.getLnLaFormC3 = function(cartesian3) {
	return VFG.Util.getLnLaFormC3(this.viewer,cartesian3);
}

VFG.Map.prototype.openWebSocket = function(ws) {
	 if ("WebSocket" in window){
	      var ws = new WebSocket(ws);
	      ws.onopen = function(){
	         ws.send("发送数据");
	         console.log("连接已关闭...");
	      };
	      ws.onmessage = function (evt) { 
	      };
	      ws.onclose = function(){ 
	         console.log("连接已关闭...");
	      };
	 }
}

VFG.Map.prototype.createEvent = function() {
	return new VFG.Event(this.viewer);
}

/**
 * 监听
 * {
 *  position:{},
 *  callback:function(Listener)
 * }
 */
VFG.Map.prototype.createListener = function(option) {
	return new VFG.Listener(this.viewer,option);
}


/**
 * 拾取对象
 */
VFG.Map.prototype.pick = function(position) {
	let obj=VFG.Util.pick(this.viewer,position);
	if(Cesium.defined(obj)){
		if(obj.id instanceof Cesium.Entity){
			return this.getById(obj.id.id);
		}else{
			return this.getById(obj.id);
		}
	}
}

VFG.Map.prototype.flyToChina = function() {
	var rectangle = Cesium.Rectangle.fromDegrees(90, -20, 110, 90);
	var center = Cesium.Rectangle.center(rectangle);//获取视角范围中心点(得到的结果为弧度Cartographic)
	var adre = Cesium.Cartographic.toCartesian(center);//把弧度转换为x,y,z（世界坐标也就是Cartographic3）
	var ellipsoid = this.viewer.scene.globe.ellipsoid;
	var cartographic = ellipsoid.cartesianToCartographic(adre);
	var lat = Cesium.Math.toDegrees(cartographic.latitude);//纬度
	var lng = Cesium.Math.toDegrees(cartographic.longitude);//经度
	this.viewer.camera.flyTo({//定位到范围中心点
	  destination: rectangle,
	  orientation: {
	     heading: Cesium.Math.toRadians(180),//1
	     pitch: Cesium.Math.toRadians(-90),
	     roll: 0
	  }
	});
}

VFG.Map.prototype.flyToPoisition = function(position) {
	if(position){
		this.viewer.camera.flyTo({//定位到范围中心点
			  destination: Cesium.Cartesian3.fromDegrees(position.x*1, position.y*1,(position.z*1)+500),
			  orientation: {
			     heading: Cesium.Math.toRadians(180),//1
			     pitch: Cesium.Math.toRadians(-90),
			     roll: 0
			  }
			});
	}
}

VFG.Map.prototype.flyToThing = function(thing) {
	if(thing){
		if(thing instanceof VFG.PModel){
			this.flyToPoisition({
				x:thing.position.x,
				y:thing.position.y,
				z:thing.getRadius(),
			});
		}
		else if(thing instanceof VFG.VModel){
			this.flyToPoisition({
				x:thing.position.x,
				y:thing.position.y,
				z:thing.getRadius(),
			});
		}else{
			this.flyToPoisition(thing.position);
		}
	}
}

VFG.Map.prototype.createPopup = function(options) {
	return new VFG.Popup(this.viewer, options)
}

VFG.Map.prototype.drawCircle = function(options) {
	return  new VFG.DrawCircle(this.viewer,options)
}

/**
 * 创建Tip对象
 */
VFG.Map.prototype.createTip = function() {
	return new VFG.Tip(this.viewer)
}

VFG.Map.prototype.getScreenToLnLa =function(position, noPickEntity) {
	return VFG.Util.getScreenToLnLa(this.viewer, position, noPickEntity) 
}

VFG.Map.prototype.hideGlobe=function() {
	this.viewer.scene.sun.show = true; 
	this.viewer.scene.moon.show = false;
	this.viewer.scene.skyBox.show = true;//关闭天空盒，否则会显示天空颜色
	this.viewer.scene.globe.show = false; //不显示地球，这条和地球透明度选一个就可以
	this.viewer.scene.globe.baseColor = new Cesium.Color(0, 0, 0, 0);
	this.viewer.scene.backgroundcolor = new Cesium.Color(0, 0, 0, 0)
};

VFG.Map.prototype.addEventListener=function(type,fun) {
	if('cameraChanged'==type){
		if(Cesium.defined(this.cameraChangedEvent)){
			this.removeEventListener(type);
		}
		this.cameraChangedEvent=fun;
		this.viewer.scene.camera.changed.addEventListener(this.cameraChangedEvent, this);
		return;
	}
	
	if('cameraMoveEnd'==type){
		if(Cesium.defined(this.cameraMoveEnd)){
			this.removeEventListener(type);
		}
		this.cameraMoveEnd=fun;
		this.viewer.scene.camera.moveEnd.addEventListener(this.cameraMoveEnd, this);
		return;
	}
	
	if('mouseMove'==type){
		this.mouseMoveFun=fun;
		return;
	}
};

VFG.Map.prototype.removeEventListener=function(type) {
	if('cameraChanged'==type){
		this.viewer.scene.camera.changed.removeEventListener(this.cameraChangedEvent, this);
		this.cameraChangedEvent=null;
		return
	}
	
	if('cameraMoveEnd'==type){
		if(Cesium.defined(this.cameraMoveEnd)){
			this.removeEventListener(type);
		}
		this.viewer.scene.camera.moveEnd.removeEventListener(this.cameraMoveEnd, this);
		this.cameraMoveEnd=null;
		return;
	}
	if('mouseMove'==type){
		this.mouseMoveFun=null;
		return;
	}
	
};

VFG.Map.prototype.getCurrentCameraInfo=function(movement) {
	let camera={};
    camera.height = this.viewer.camera.positionCartographic.height.toFixed(1);
    camera.heading = Cesium.Math.toDegrees(this.viewer.camera.heading).toFixed(0);
    camera.pitch = Cesium.Math.toDegrees(this.viewer.camera.pitch).toFixed(0);
    camera.pitch = Cesium.Math.toDegrees(this.viewer.camera.pitch).toFixed(0);
	return camera
};

/**
 * 获取鼠标位置经纬度
 */
VFG.Map.prototype.getCurrentMousePosition=function(position) {
	let locationData={};
	var cartesian = VFG.Util.getScreenToC3(this.viewer, position);
	if (!cartesian) return;
	var cartographic = Cesium.Cartographic.fromCartesian(cartesian);
	locationData.z = (cartographic.height / this.viewer.scene.terrainExaggeration).toFixed(1);
	locationData.x = Cesium.Math.toDegrees(cartographic.longitude);
	locationData.y = Cesium.Math.toDegrees(cartographic.latitude);
	return locationData
};


//提取地球视域边界
VFG.Map.prototype.getExtent=function(opts) {
    opts = opts || {};

    // 范围对象
    var extent = {
        xmin: 0,
        xmax: 0,
        ymin: 0,
        ymax: 0
    };

    // var rectangle = viewer.camera.computeViewRectangle(); //不支持二维模式
    // if (rectangle == null) return null; 
    // var extent = formatRectangle(rectangle);  // 范围对象

    //默认值：中国区域
    extent = {
        xmin: 70,
        xmax: 140,
        ymin: 0,
        ymax: 55,
        height: 0
    };

    // 得到当前三维场景
    var viewer = this.viewer;
    var scene = viewer.scene;

    // 得到当前三维场景的椭球体
    var ellipsoid = scene.globe.ellipsoid;
    var canvas = scene.canvas;

    // canvas左上角
    var car3_lt = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(0, 0), ellipsoid);
    if (car3_lt) {
        // 在椭球体上
        var carto_lt = ellipsoid.cartesianToCartographic(car3_lt);
        extent.xmin = Cesium.Math.toDegrees(carto_lt.longitude);
        extent.ymax = Cesium.Math.toDegrees(carto_lt.latitude);
        extent.height = Math.max(extent.height, carto_lt.height);
    } else {
        // 不在椭球体上 
        var xMax = canvas.width / 2;
        var yMax = canvas.height / 2;

        var car3_lt2;
        // 这里每次10像素递加，一是10像素相差不大，二是为了提高程序运行效率
        for (var yIdx = 0; yIdx <= yMax; yIdx += 10) {
            var xIdx = yIdx <= xMax ? yIdx : xMax;
            car3_lt2 = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(xIdx, yIdx), ellipsoid);
            if (car3_lt2) break;
        }
        if (car3_lt2) {
            var carto_lt = ellipsoid.cartesianToCartographic(car3_lt2);
            extent.xmin = Cesium.Math.toDegrees(carto_lt.longitude);
            extent.ymax = Cesium.Math.toDegrees(carto_lt.latitude);
            extent.height = Math.max(extent.height, carto_lt.height);
        }
    }

    // canvas右下角
    var car3_rb = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(canvas.width, canvas.height), ellipsoid);
    if (car3_rb) {
        // 在椭球体上
        var carto_rb = ellipsoid.cartesianToCartographic(car3_rb);
        extent.xmax = Cesium.Math.toDegrees(carto_rb.longitude);
        extent.ymin = Cesium.Math.toDegrees(carto_rb.latitude);
        extent.height = Math.max(extent.height, carto_rb.height);
    } else {
        // 不在椭球体上
        var xMax = canvas.width / 2;
        var yMax = canvas.height / 2;

        var car3_rb2;
        // 这里每次10像素递减，一是10像素相差不大，二是为了提高程序运行效率
        for (var yIdx = canvas.height; yIdx >= yMax; yIdx -= 10) {
            var xIdx = yIdx >= xMax ? yIdx : xMax;
            car3_rb2 = viewer.camera.pickEllipsoid(new Cesium.Cartesian2(xIdx, yIdx), ellipsoid);
            if (car3_rb2) break;
        }
        if (car3_rb2) {
            var carto_rb = ellipsoid.cartesianToCartographic(car3_rb2);
            extent.xmax = Cesium.Math.toDegrees(carto_rb.longitude);
            extent.ymin = Cesium.Math.toDegrees(carto_rb.latitude);
            extent.height = Math.max(extent.height, carto_rb.height);
        }
    }

    
    //交换
    if (extent.xmax < extent.xmin) {
        var temp = extent.xmax;
        extent.xmax = extent.xmin;
        extent.xmin = temp;
    }
    if (extent.ymax < extent.ymin) {
        var temp = extent.ymax;
        extent.ymax = extent.ymin;
        extent.ymin = temp;
    }

    //缩放
    if (opts.scale) {
        var old_xmin = extent.xmin;
        var old_xmax = extent.xmax;
        var old_ymin = extent.ymin;
        var old_ymax = extent.ymax;

        //限定最大倍数
        if (opts.scale > 3) opts.scale = 3;
        if (opts.scale < -3) opts.scale = -3;

        var stepx = (extent.xmax - extent.xmin) * opts.scale;
        extent.xmin -= stepx;
        extent.xmax += stepx;
        var stepy = (extent.ymax - extent.ymin) * opts.scale;
        extent.ymin -= stepy;
        extent.ymax += stepy;

        //如果超出地球范围，还原放大的值
        if (extent.xmin < -180 || extent.xmax > 180 || extent.ymin < -90 || extent.ymax > 90) {
            extent.xmin = old_xmin;
            extent.xmax = old_xmax;
            extent.ymin = old_ymin;
            extent.ymax = old_ymax;
        }
    }

    return extent;
}

//计算地图缩放等级
VFG.Map.prototype.getZoomLevel=function() {
	 let h =this.viewer.camera.positionCartographic.height.toFixed(1)
    if (h <= 100) { //0.01
        return 19;
    } else if (h <= 300) { //0.02
        return 18;
    } else if (h <= 660) { //0.05
        return 17;
    } else if (h <= 1300) { //0.1
        return 16;
    } else if (h <= 2600) { //0.2
        return 15;
    } else if (h <= 6400) { //0.5
        return 14;
    } else if (h <= 13200) { //1
        return 13;
    } else if (h <= 26000) { //2
        return 12;
    } else if (h <= 67985) { //5
        return 11;
    } else if (h <= 139780) { //10
        return 10;
    } else if (h <= 250600) { //20
        return 9;
    } else if (h <= 380000) { //30
        return 8;
    } else if (h <= 640000) { //50
        return 7;
    } else if (h <= 1280000) { //100
        return 6;
    } else if (h <= 2600000) { //200
        return 5;
    } else if (h <= 6100000) { //500
        return 4;
    } else if (h <= 11900000) { //1000
        return 3;
    } else {
        return 2;
    }
}

VFG.Map.prototype.wgs84ToWindowCoordinates=function(position){
	let cartesian3=Cesium.Cartesian3.fromDegrees(position.x*1, position.y*1, position.z*1);
	if(cartesian3){
		return  Cesium.SceneTransforms.wgs84ToWindowCoordinates(this.viewer.scene, cartesian3)
	}
	return null;
}




/**
 * 销毁对象
 */
VFG.Map.prototype.destroy = function() {
  if (this.viewer) {
    this.viewer.destroy();
    this.viewer = null;
  }
}
